package com.sxfq.loansupermarket.backsystem.modular.sharesHolding.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.common.constant.factory.PageFactory;
import com.sxfq.loansupermarket.backsystem.common.controller.BaseController;
import com.sxfq.loansupermarket.backsystem.common.exception.BizExceptionEnum;
import com.sxfq.loansupermarket.backsystem.common.exception.BussinessException;
import com.sxfq.loansupermarket.backsystem.core.log.LogManager;
import com.sxfq.loansupermarket.backsystem.core.log.factory.LogTaskFactory;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.support.DateTimeKit;
import com.sxfq.loansupermarket.backsystem.core.support.HttpKit;
import com.sxfq.loansupermarket.backsystem.core.util.ToolUtil;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.sharesHolding.entity.SearchSharesArePeopleInfo;
import com.sxfq.loansupermarket.backsystem.modular.sharesHolding.entity.SharesAreCompanyInfo;
import com.sxfq.loansupermarket.backsystem.modular.sharesHolding.entity.SharesArePeopleInfo;
import com.sxfq.loansupermarket.backsystem.modular.sharesHolding.service.SharesArePeopleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 质量管理控制器
 *
 * @author fengshuonan
 * @Date 2019-09-01 15:34:05
 */
@Controller
@RequestMapping("/sharesArePeople")
public class SharesArePeopleController extends BaseController {
    @Autowired
    SharesArePeopleService service;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SqlMapper sqlMapper;


    private String PREFIX = "/sharesHolding/";

    /**
     * 跳转到量管理首页
     */
    @RequestMapping("")
    public String index(Model model) {
        String tableHead = redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"Abbreviation",0)+"";
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        return PREFIX + "sharesArePeople.html";
    }

    @RequestMapping("/list")
    @ResponseBody
    public Object list(SearchSharesArePeopleInfo info, HttpServletRequest request) {
        Page<SharesArePeopleInfo> page = new PageFactory<SharesArePeopleInfo>().defaultPage();
        try {
            info.setStartIndex(page.getSize() * (page.getCurrent() - 1));
            info.setLimitIndex(page.getSize());
            page.setCurrent(0);
            List<SharesArePeopleInfo> list = service.getList(page, info);
            if (list.size() == 0) {
                info.setStartIndex(0);
                list = service.getList(page, info);
            }
            page.setRecords(list);
            page.setTotal(service.getQueryPagesCount(page, info));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.packForBT(page);
    }

    //跳到添加页面
    @RequestMapping("/sharesArePeopleAdd")
    public String sharesArePeopleAdd() {
        return PREFIX + "sharesArePeopleAdd.html";
    }

    //添加
    @RequestMapping(value = "/sharesArePeopleAddOk", method = RequestMethod.POST)
    @ResponseBody
    public int sharesArePeopleAddOk(String peopleName, String sex,String age, String phone,String email,
                                    String remarks) {
        String sql2 = "INSERT INTO share_sare_people_info ( people_name, sex, age, phone, email, createtime, remarks) VALUES " +
                " ('"+peopleName+"', '"+sex+"', '"+age+"', '"+phone+"', '"+email+"', '"+ DateTimeKit.now() +"', '"+remarks+"'); ";
        return sqlMapper.insert(sql2);
    }

    //跳到编辑页面
    @RequestMapping("/sharesArePeopleEdit/{id}")
    public String sharesArePeopleEdit(@PathVariable String id,Model model) throws Exception {
        if (ToolUtil.isEmpty(id)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        String sql ="select * from share_sare_people_info where id='"+id+"' ";
        SharesArePeopleInfo info=sqlMapper.selectOne(sql,SharesArePeopleInfo.class);
        model.addAttribute("id",id);
        model.addAttribute("info",info);
        return PREFIX + "sharesArePeopleEdit.html";
    }

    //编辑
    @RequestMapping(value = "/sharesArePeopleEditOk", method = RequestMethod.POST)
    @ResponseBody
    public int sharesArePeopleEditOk(String peopleName, String sex,String age, String phone,String email,String id,String remarks) {
        if (ToolUtil.isEmpty(id)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        String sql = "update share_sare_people_info set people_name='"+peopleName+"', sex='"+sex+"', age='"+age+"', phone='"+phone+"', email='"+email+"', remarks='"+remarks+"' " +
                " where id ='"+id+"' ";
        return sqlMapper.update(sql);
    }

    //删除
    @RequestMapping(value = "/sharesArePeopleDelete/{id}")
    @ResponseBody
    public int sharesArePeopleDelete(@PathVariable String id) throws Exception {
        if (ToolUtil.isEmpty(id)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        String sql = "delete from share_sare_people_info where id ='"+id+"' ";
        return sqlMapper.delete(sql);
    }

}
